<?php
namespace Swiftx\Bundle\WeChat\Model\Seller;
use Swiftx\Bundle\WeChat\Interfaces\Common\EntityModel;
use Swiftx\Interfaces\Orm\Pager;

/**
 * 相册模型类
 */
class Album extends EntityModel {

    /**
     * 所属企业
     * @var int
     * @orm constraint no-null
     * @orm type char(36)
     * @orm column Company
     */
    protected $company;

    /**
     * 相册标题
     * @var string
     * @orm constraint no-null
     * @orm type varchar(100)
     * @orm column Title
     */
    protected $title;

    /**
     * 创建时间
     * @var int
     * @orm constraint no-null
     * @orm type int(11)
     * @orm column CreateTime
     */
    protected $createTime;

    /**
     * 默认属性生成
     */
    public function __construct(){
        parent::__construct();
        $this->createTime = time();
    }

    /**
     * 查看所属企业ID
     * @return string
     */
    public function getCompanyID():string {
        return $this->company;
    }

    /**
     * 设置所属公司
     * @param string $id
     */
    public function setCompany(string $id){
        $this->company = $id;
    }

    /**
     * 设置标题
     * @param string $value
     */
    public function setTitle(string $value){
        $this->title = $value;
    }

    /**
     * 获取标题
     * @return string
     */
    public function getTitle():string {
        return $this->title;
    }

    /**
     * 统计图片数量
     * @return int
     */
    public function countImages():int {
        $query = $this->entityManager->query(Image::class,'EqualByAlbum');
        $query->bind('album', $this->getID());
        return $query->count();
    }

    /**
     * 获取图片集合
     * @return Image[]
     */
    public function getImages():array {
        $query = $this->entityManager->query(Image::class,'EqualByAlbum');
        $query->bind('album', $this->getID());
        return $query->list();
    }

    /**
     * 获取图片集合
     * @param int $start
     * @param int $number
     * @return array|Image[]
     */
    public function getImagesLimit(int $start, int $number):array {
        $query = $this->entityManager->query(Image::class,'EqualByAlbum');
        $query->bind('album', $this->getID());
        return $query->list($number, $start);
    }

    /**
     * 获取图片集合
     * @param int $current
     * @param int $number
     * @param bool $sync
     * @return Pager
     */
    public function getImagePage(int $current, int $number, bool $sync=true):Pager {
        $query = $this->entityManager->query(Image::class,'EqualByAlbum');
        $query->bind('album', $this->getID());
        return $query->page($current, $number, $sync);
    }

}